package com.hspedu.newData.LCR;

import com.hspedu.newData.dmsxl.ds.TreeNode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-26 
 * @Version: 1.0
 */

public class LCR46二叉树的右视图 {

    public List<Integer> rightSideView(TreeNode root) {

        List<Integer> list = new ArrayList<>();
        if (root==null) return list;

        Queue<TreeNode> queue = new ArrayDeque<>();
        queue.offer(root);

        while (!queue.isEmpty()){

            int len = queue.size();

            while (len>0){
                TreeNode node = queue.poll();

                if (len==1){
                    list.add(node.val);
                }
                if (node.left!=null) queue.offer(node.left);
                if (node.right!=null) queue.offer(node.right);
                len--;
            }
        }
        return list;
    }

}
